<template>
  <div class="stock-status">
    <el-card>
      <div slot="header" class="clearfix">
        <span>库存状态</span>
      </div>

      <!-- 搜索栏 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="商品名称">
          <el-input v-model="searchForm.name" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select v-model="searchForm.category" placeholder="请选择分类">
            <el-option label="饮料" value="drink"></el-option>
            <el-option label="零食" value="snack"></el-option>
            <el-option label="日用品" value="daily"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库存状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="正常" value="normal"></el-option>
            <el-option label="预警" value="warning"></el-option>
            <el-option label="缺货" value="out"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 库存统计卡片 -->
      <el-row :gutter="20" class="statistics-cards">
        <el-col :span="6">
          <el-card shadow="hover">
            <div class="statistics-item">
              <div class="title">总库存商品数</div>
              <div class="value">{{ totalProducts }}</div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover">
            <div class="statistics-item">
              <div class="title">库存预警数</div>
              <div class="value warning">{{ warningProducts }}</div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover">
            <div class="statistics-item">
              <div class="title">缺货商品数</div>
              <div class="value danger">{{ outOfStockProducts }}</div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover">
            <div class="statistics-item">
              <div class="title">库存总价值</div>
              <div class="value">¥{{ totalValue.toFixed(2) }}</div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 库存列表 -->
      <el-table :data="stockList" style="width: 100%" border>
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="name" label="商品名称" width="150"></el-table-column>
        <el-table-column prop="category" label="分类" width="100">
          <template slot-scope="scope">
            <el-tag>{{ getCategoryName(scope.row.category) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="stock" label="当前库存" width="100"></el-table-column>
        <el-table-column prop="minStock" label="最低库存" width="100"></el-table-column>
        <el-table-column prop="maxStock" label="最高库存" width="100"></el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="getStockStatusType(scope.row.status)">
              {{ getStockStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="lastUpdateTime" label="最后更新时间" width="180"></el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleStockIn(scope.row)">入库</el-button>
            <el-button size="mini" type="text" @click="handleStockOut(scope.row)">出库</el-button>
            <el-button size="mini" type="text" @click="handleAdjust(scope.row)">调整</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 入库对话框 -->
    <el-dialog title="商品入库" :visible.sync="stockInVisible" width="500px">
      <el-form :model="stockInForm" :rules="stockInRules" ref="stockInForm" label-width="100px">
        <el-form-item label="商品名称">
          <span>{{ currentProduct.name }}</span>
        </el-form-item>
        <el-form-item label="当前库存">
          <span>{{ currentProduct.stock }}</span>
        </el-form-item>
        <el-form-item label="入库数量" prop="quantity">
          <el-input-number v-model="stockInForm.quantity" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item label="入库原因" prop="reason">
          <el-input type="textarea" v-model="stockInForm.reason"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="stockInVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleStockInSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 出库对话框 -->
    <el-dialog title="商品出库" :visible.sync="stockOutVisible" width="500px">
      <el-form :model="stockOutForm" :rules="stockOutRules" ref="stockOutForm" label-width="100px">
        <el-form-item label="商品名称">
          <span>{{ currentProduct.name }}</span>
        </el-form-item>
        <el-form-item label="当前库存">
          <span>{{ currentProduct.stock }}</span>
        </el-form-item>
        <el-form-item label="出库数量" prop="quantity">
          <el-input-number v-model="stockOutForm.quantity" :min="1" :max="currentProduct.stock"></el-input-number>
        </el-form-item>
        <el-form-item label="出库原因" prop="reason">
          <el-input type="textarea" v-model="stockOutForm.reason"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="stockOutVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleStockOutSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 库存调整对话框 -->
    <el-dialog title="库存调整" :visible.sync="adjustVisible" width="500px">
      <el-form :model="adjustForm" :rules="adjustRules" ref="adjustForm" label-width="100px">
        <el-form-item label="商品名称">
          <span>{{ currentProduct.name }}</span>
        </el-form-item>
        <el-form-item label="当前库存">
          <span>{{ currentProduct.stock }}</span>
        </el-form-item>
        <el-form-item label="调整数量" prop="quantity">
          <el-input-number v-model="adjustForm.quantity"></el-input-number>
        </el-form-item>
        <el-form-item label="调整原因" prop="reason">
          <el-input type="textarea" v-model="adjustForm.reason"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="adjustVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdjustSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'StockStatusView',
  data() {
    return {
      searchForm: {
        name: '',
        category: '',
        status: ''
      },
      totalProducts: 1000,
      warningProducts: 20,
      outOfStockProducts: 5,
      totalValue: 50000.00,
      stockList: [
        {
          id: 1,
          name: '可口可乐',
          category: 'drink',
          stock: 100,
          minStock: 50,
          maxStock: 200,
          status: 'normal',
          lastUpdateTime: '2024-03-03 12:00:00'
        },
        {
          id: 2,
          name: '康师傅方便面',
          category: 'snack',
          stock: 30,
          minStock: 50,
          maxStock: 100,
          status: 'warning',
          lastUpdateTime: '2024-03-03 12:00:00'
        }
      ],
      currentPage: 1,
      pageSize: 10,
      total: 100,
      stockInVisible: false,
      stockOutVisible: false,
      adjustVisible: false,
      currentProduct: {},
      stockInForm: {
        quantity: 1,
        reason: ''
      },
      stockOutForm: {
        quantity: 1,
        reason: ''
      },
      adjustForm: {
        quantity: 0,
        reason: ''
      },
      stockInRules: {
        quantity: [
          { required: true, message: '请输入入库数量', trigger: 'blur' }
        ],
        reason: [
          { required: true, message: '请输入入库原因', trigger: 'blur' }
        ]
      },
      stockOutRules: {
        quantity: [
          { required: true, message: '请输入出库数量', trigger: 'blur' }
        ],
        reason: [
          { required: true, message: '请输入出库原因', trigger: 'blur' }
        ]
      },
      adjustRules: {
        quantity: [
          { required: true, message: '请输入调整数量', trigger: 'blur' }
        ],
        reason: [
          { required: true, message: '请输入调整原因', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getCategoryName(category) {
      const names = {
        drink: '饮料',
        snack: '零食',
        daily: '日用品'
      }
      return names[category] || category
    },
    getStockStatusType(status) {
      const types = {
        normal: 'success',
        warning: 'warning',
        out: 'danger'
      }
      return types[status] || 'info'
    },
    getStockStatusText(status) {
      const texts = {
        normal: '正常',
        warning: '预警',
        out: '缺货'
      }
      return texts[status] || '未知'
    },
    handleSearch() {
      // TODO: 实现搜索逻辑
      console.log('搜索条件：', this.searchForm)
    },
    resetSearch() {
      this.searchForm = {
        name: '',
        category: '',
        status: ''
      }
    },
    handleSizeChange(val) {
      this.pageSize = val
      // TODO: 重新加载数据
    },
    handleCurrentChange(val) {
      this.currentPage = val
      // TODO: 重新加载数据
    },
    handleStockIn(row) {
      this.currentProduct = row
      this.stockInForm = {
        quantity: 1,
        reason: ''
      }
      this.stockInVisible = true
    },
    handleStockOut(row) {
      this.currentProduct = row
      this.stockOutForm = {
        quantity: 1,
        reason: ''
      }
      this.stockOutVisible = true
    },
    handleAdjust(row) {
      this.currentProduct = row
      this.adjustForm = {
        quantity: row.stock,
        reason: ''
      }
      this.adjustVisible = true
    },
    handleStockInSubmit() {
      this.$refs.stockInForm.validate((valid) => {
        if (valid) {
          // TODO: 实现入库逻辑
          this.stockInVisible = false
          this.$message.success('入库成功')
        }
      })
    },
    handleStockOutSubmit() {
      this.$refs.stockOutForm.validate((valid) => {
        if (valid) {
          // TODO: 实现出库逻辑
          this.stockOutVisible = false
          this.$message.success('出库成功')
        }
      })
    },
    handleAdjustSubmit() {
      this.$refs.adjustForm.validate((valid) => {
        if (valid) {
          // TODO: 实现库存调整逻辑
          this.adjustVisible = false
          this.$message.success('调整成功')
        }
      })
    }
  }
}
</script>

<style scoped>
.stock-status {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.statistics-cards {
  margin-bottom: 20px;
}

.statistics-item {
  text-align: center;
}

.statistics-item .title {
  font-size: 14px;
  color: #666;
  margin-bottom: 10px;
}

.statistics-item .value {
  font-size: 24px;
  color: #333;
  font-weight: bold;
}

.statistics-item .value.warning {
  color: #E6A23C;
}

.statistics-item .value.danger {
  color: #F56C6C;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style> 